#ifndef GB28181_RECORD_H
#define GB28181_RECORD_H

#include <string>
#include <vector>
#include <functional>
#include <chrono>

extern "C" {
#include <eXosip2/eXosip.h>
#include <osipparser2/osip_message.h>
}

namespace El {
namespace GB28181 {

// Forward declarations
struct SipServerConfig;

// Record type enum
enum class RecordType {
    ALL = 0,        // 全部
    TIME = 1,       // 时间段录像
    ALARM = 2,      // 报警录像
    MANUAL = 3,     // 手动录像
    MOTION = 4      // 移动侦测录像
};

// Record info structure
struct RecordInfo {
    std::string device_id;      // 设备编码
    std::string channel_id;     // 通道编码
    std::string name;           // 录像名称
    std::string file_path;      // 文件路径
    std::string start_time;     // 开始时间 (YYYY-MM-DDTHH:MM:SS)
    std::string end_time;       // 结束时间 (YYYY-MM-DDTHH:MM:SS)
    uint64_t file_size;         // 文件大小 (字节)
    RecordType type;            // 录像类型
    std::string address;        // 录像地址
    bool secrecy;               // 保密标识
    
    RecordInfo() : file_size(0), type(RecordType::ALL), secrecy(false) {}
};

// Record query info
struct RecordQueryInfo {
    std::string device_id;      // 设备编码
    std::string channel_id;     // 通道编码 (可选)
    std::string start_time;     // 查询开始时间
    std::string end_time;       // 查询结束时间
    RecordType type;            // 录像类型
    std::string sn;             // 命令序列号
    
    RecordQueryInfo() : type(RecordType::ALL) {}
};

// Record query callback
using RecordQueryCallback = std::function<std::vector<RecordInfo>(const RecordQueryInfo& query)>;

// Record query handler for GB28181
class GB28181Record {
public:
    // Process RecordInfo query message
    static void ProcessRecordQuery(eXosip_event_t *event, eXosip_t *exosip_context,
                                 const SipServerConfig &config,
                                 RecordQueryCallback queryCallback);
    
    // Send RecordInfo query response
    static void SendRecordQueryResponse(eXosip_t *exosip_context, eXosip_event_t *event,
                                       const std::vector<RecordInfo> &records,
                                       const std::string &sn,
                                       const std::string &device_id);
    
    // Parse RecordInfo query XML
    static bool ParseRecordQueryXML(const std::string &xml, RecordQueryInfo &query);
    
    // Create RecordInfo response XML
    static std::string CreateRecordResponseXML(const std::vector<RecordInfo> &records,
                                              const std::string &device_id,
                                              const std::string &sn);
    
    // Record type to string
    static std::string RecordTypeToString(RecordType type);
    
    // String to record type
    static RecordType StringToRecordType(const std::string &type_str);
    
    // Validate time format (ISO 8601)
    static bool ValidateTimeFormat(const std::string &time_str);
    
    // Convert time string to chrono time_point
    static std::chrono::system_clock::time_point ParseTimeString(const std::string &time_str);
    
    // Convert chrono time_point to GB28181 time string
    static std::string FormatTimeString(const std::chrono::system_clock::time_point &time_point);

private:
    // Extract XML value by tag
    static std::string ExtractXMLValue(const std::string &xml, const std::string &tag);
    
    // Validate record query parameters
    static bool ValidateRecordQuery(const RecordQueryInfo &query);
    
    // Create single record item XML
    static std::string CreateRecordItemXML(const RecordInfo &record);
    
    // Format file size to human readable string
    static std::string FormatFileSize(uint64_t size);
};

} // namespace GB28181
} // namespace El

#endif // GB28181_RECORD_H